<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="username" prop="username">
      <el-input v-model="dataForm.username" placeholder="username"></el-input>
    </el-form-item>
    <el-form-item label="password" prop="password">
      <el-input v-model="dataForm.password" placeholder="password"></el-input>
    </el-form-item>
    <el-form-item label="email" prop="email">
      <el-input v-model="dataForm.email" placeholder="email"></el-input>
    </el-form-item>
    <el-form-item label="mobile" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="mobile"></el-input>
    </el-form-item>
    <el-form-item label="age" prop="age">
      <el-input v-model.number="dataForm.age" placeholder="age"></el-input>
    </el-form-item>
    <el-form-item label="createTime" prop="createTime">
      <el-date-picker
        v-model="dataForm.createTime"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        format="yyyy-MM-dd HH:mm:ss"
        placeholder="请选择 createTime">
      </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          email: '',
          mobile: '',
          age: '',
          createTime: ''
        },
        dataRule: {
          username: [
            { required: true, message: 'username不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: 'password不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: 'email不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: 'mobile不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: 'age 必须是数字', trigger: 'blur', type: 'number' }
          ],
          createTime: [
            { required: true, message: 'createTime不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/portal/portaluser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.portalUser.username
                this.dataForm.password = data.portalUser.password
                this.dataForm.email = data.portalUser.email
                this.dataForm.mobile = data.portalUser.mobile
                this.dataForm.age = data.portalUser.age
                this.dataForm.createTime = data.portalUser.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/portal/portaluser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'email': this.dataForm.email,
                'mobile': this.dataForm.mobile,
                'age': this.dataForm.age,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
